from .db_context import DBConnection
from ..model.assignment import Assignment


# 发布作业/签到时，将作业/签到插入Assignment表中
async def insert_assignment(assignment: Assignment):
    conn = DBConnection().connection
    cur = conn.cursor()
    cur.execute("INSERT INTO Assignment (gid, name, endTimeStamp, type) VALUES(?,?,?,?)",
                (assignment.gid, assignment.name, assignment.endTimestamp, assignment.type_tag))

    cur.close()
    conn.commit()


# 获取作业/签到
async def getAssignmentOfClass(gid, tag):
    cur = DBConnection().connection.cursor()
    cur.execute('''
    SELECT *
    FROM Assignment
    WHERE gid=:gid AND type=:tag
    ''', {"gid": gid, "tag": tag})
    result = cur.fetchall()
    cur.close()
    return result

#获取AssignmentID
async def get_assignment_id(gid, name):
    cur = DBConnection().connection.cursor()
    cur.execute('''
    SELECT id
    FROM Assignment
    WHERE gid=:gid AND name=:name
    ''', {"gid": gid, "name": name})
    result = cur.fetchone()
    cur.close()
    return result
